//
//  RTDeviceInfoController.m
//  RBToyAPI
//
//  Created by baxiang on 2018/2/1.
//  Copyright © 2018年 roobo. All rights reserved.
//

#import "RTDeviceInfoController.h"
#import "RTDetailInfoTableCell.h"
#import "RTUserListController.h"
@interface RTDeviceInfoController ()
@property(nonatomic,copy)RBDevicesDetail *currDevice;
@end

@implementation RTDeviceInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[RTDetailInfoTableCell class] forCellReuseIdentifier:NSStringFromClass([RTDetailInfoTableCell class])];
    [self fetchDeviceInfo];
}

-(void)fetchDeviceInfo{
    @weakify(self);
    [RBDeviceApi getDeviceDetail:^(RBDevicesDetail *detailModel, NSError *error) {
        @strongify(self);
        self.currDevice = detailModel;
        [self.tableView reloadData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 4;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RTDetailInfoTableCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([RTDetailInfoTableCell class]) forIndexPath:indexPath];
    if (indexPath.row==0) {
        cell.namelab.text = @"设备名称";
        cell.contentlab.text = self.currDevice.name;
    }else if (indexPath.row==1){
        cell.namelab.text = @"设备在线状态";
        cell.contentlab.text = self.currDevice.online?@"在线":@"离线";
    }else if (indexPath.row==2){
        cell.namelab.text = @"设备电量";
        cell.contentlab.text = [NSString stringWithFormat:@"%ld",self.currDevice.battery];
    }else if (indexPath.row==3){
        cell.namelab.text = @"设备用户";
        cell.contentlab.text = nil;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 3) {
        RTUserListController *userListVC = [RTUserListController new];
        userListVC.users = self.currDevice.users;
        [self.navigationController pushViewController:userListVC animated:YES];
    }
    
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
